package com.hq.talk.view.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.hq.lib_comview.common.utils.DialogUtils;
import com.hq.lib_comview.view.scprogress.SVProgressHUD;
import com.hq.talk.R;
import com.hq.talk.common.Session;
import com.hq.talk.presenter.IPresenter;
import com.hq.talk.presenter.IViewController;
import com.hq.talk.presenter.I_SkipActivity;
import com.hq.talk.view.ui.LoginAty;

import butterknife.ButterKnife;

/**
 * basefragment 的基类
 */
public abstract class BaseFrg extends Fragment implements IViewController, I_SkipActivity {

    protected IPresenter presenter;

    protected boolean isViewInited = false;

    public Activity frg;

    private boolean isAddIn = false;
    private Dialog dialog;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.frg = getActivity();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = loadView(inflater);
        if (view == null) {
            view = loadView(inflater, null);
        }
        ButterKnife.bind(this, view);
        initPresenter();
        if (presenter != null) {
            presenter.attachView(this);
        }
        initView();
        initData();
        isViewInited = true;
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isAddIn) {
            if (isVisible()) {
                onVisible();
            }
        } else {
            if (getUserVisibleHint()) {
                onVisible();
            }
        }
    }

    /**
     * Fragment可见时回掉
     */
    protected void onVisible() {
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isViewInited = false;
        if (presenter != null) {
            presenter.detachView();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 用户是否登录
     *
     * @param isShowDialog 是否显示弹出框 true 显示
     */
    public boolean isLogin(boolean isShowDialog) {
        if (Session.getUserIsLogin()) {
            return true;
        } else {
            if (isShowDialog) {
                showLoginDialog();
            } else {
                showActivity(frg, LoginAty.class);
            }
            return false;
        }
    }

    /**
     * 弹出登录提示框
     */
    public void showLoginDialog() {
        if (isDetached() || getActivity() == null) {
            return;
        }

        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        View view = frg.getLayoutInflater().inflate(R.layout.dialog_login_remind, null, false);
        TextView mTxtCancel = (TextView) view.findViewById(R.id.id_login_cancel_txt);
        mTxtCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView mTxtConfirm = (TextView) view.findViewById(R.id.id_login_confirm_txt);
        mTxtConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                showActivity(frg, LoginAty.class);
            }
        });

        dialog = DialogUtils.selfDialog(frg, view, true);
        dialog.show();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isViewInited) {
            if (getUserVisibleHint()) {
                onVisible();
            } else {
                onInVisible();
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (isViewInited) {
            if (!isHidden()) {
                onVisible();
            } else {
                onInVisible();
            }
        }
    }

    /**
     * Fragment隐藏时回掉
     */
    protected void onInVisible() {
    }

    /**
     * 弹出框消失
     */
    public void SVProgressHUDDismiss(Context context) {
        if (null == context) {
            return;
        }
        if (SVProgressHUD.isShowing(context)) {
            SVProgressHUD.dismiss(context);
        }
    }

    @Nullable
    @Override
    public View loadView(LayoutInflater inflater) {
        return null;
    }

    @Nullable
    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return null;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {

    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {

    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag, boolean direction, int size) {

    }

    @Override
    public boolean hasDataInPage() {
        return false;
    }

    @Override
    public void handlerByPage(int flag1, int flag2, boolean loadMore, String string) {

    }

    @Override
    public void handlerByPage(int flag1, int flag2, boolean loadMore, int size, String string) {

    }

    @Override
    public void skipActivity(Activity aty, Class<?> cls) {
        showActivity(aty, cls);
        aty.finish();
    }

    @Override
    public void skipActivity(Activity aty, Intent it) {
        showActivity(aty, it);
        aty.finish();
    }

    @Override
    public void skipActivity(Activity aty, Class<?> cls, Bundle extras) {
        showActivity(aty, cls, extras);
        aty.finish();
    }

    @Override
    public void showActivity(Activity aty, Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(aty, cls);
        aty.startActivity(intent);
    }

    @Override
    public void showActivity(Activity aty, Intent it) {
        aty.startActivity(it);
    }

    @Override
    public void showActivity(Activity aty, Class<?> cls, Bundle extras) {
        Intent intent = new Intent();
        intent.putExtras(extras);
        intent.setClass(aty, cls);
        aty.startActivity(intent);
    }

    //设置上次刷新的时间
    //mRefresh.restoreLastRefreshTime(lastRefreshTime);
    //XRefreshView不仅支持手势下拉刷新，也支持按钮点击开始刷新，
    //mRefresh.startRefresh();

    /**
     * @param mRefresh       刷新控件
     * @param isUpRefresh    是否可以上拉刷新
     * @param isLoadMore     是否加载更多
     * @param isAutoRefresh  是否可以自动刷新
     * @param isAutoLoadMore 是否滑动到底部自动加载更多
     */
    public void setXRefreshView(XRefreshView mRefresh, boolean isUpRefresh, boolean isLoadMore, boolean isAutoRefresh, boolean isAutoLoadMore, View mHeadView, View mFootView) {
        // 设置是否可以上拉刷新
        mRefresh.setPullLoadEnable(isUpRefresh);
        //是否加载更多
        mRefresh.setPullLoadEnable(isLoadMore);
        //设置是否可以自动刷新
        mRefresh.setAutoRefresh(isAutoRefresh);
        //滑动到底部自动加载更多,目前只做了Abslistview和Recylerview的,true会自动加载更多.
        // false则相反。传入false的话，如果是listview，需要上拉才能加载更多，如果是Recylerview，则会需要点击才能加载更多。就像这样
        mRefresh.setAutoLoadMore(isAutoLoadMore);

        //如果需要在手指横向移动的时候，让XRefreshView不拦截事件
        mRefresh.setMoveForHorizontal(true);
        //设置当下拉刷新完成以后，headerview和footerview被固定的时间
        //注:考虑到ui效果，只有时间大于1s的时候，footerview被固定的效果才会生效
        mRefresh.setPinnedTime(1000);
        //刷新时，不让里面的列表上下滑动
        mRefresh.setPinnedContent(false);
        if (null != mHeadView) {
            mRefresh.setCustomHeaderView(mHeadView);
        }
        if (null != mFootView) {
            mRefresh.setCustomFooterView(mFootView);
        }
    }

    /**
     * 停止下拉刷新
     *
     * @param mRefresh
     * @param time
     */
    public void stopRefresh(final XRefreshView mRefresh, int time) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRefresh.stopRefresh();
            }
        }, time);
    }

    /**
     * 停止加载更多
     *
     * @param mRefresh
     * @param time
     */
    public void stopLoadMore(final XRefreshView mRefresh, int time) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRefresh.stopLoadMore();
            }
        }, time);
    }
}
